package com.px.user.mapper;

import com.px.user.entity.Room;
import com.px.user.entity.RoomMarquee;
import com.px.user.entity.TeacherVideo;
import com.px.user.entity.Vote;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Repository
public interface RoomMapper {

    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert("insert into room (creatorId,adminId,voteContent,up,down,flat,pcu) values " +
            "(#{creatorId},#{adminId},#{voteContent},#{up},#{down},#{flat},#{pcu})")
    void insert(Room room);

    @Delete("delete from room where id=#{id}")
    void delete(Long id);

    @Select("select room.*,user.username,user.nickname FROM room,user where room.adminId=user.id and room.id=#{id}")
    @Results({
            @Result(property = "adminUsername", column = "username"),
            @Result(property = "adminNickname", column = "nickname")
    })
    Room findById(Long id);

    @Transactional
    @Update("update room set voteContent=ifnull(#{voteContent},voteContent),up=ifnull(#{up},up)," +
            "down=ifnull(#{down},down),flat=ifnull(#{flat},flat),adminId=ifnull(#{adminId},adminId)," +
            "pcu=ifnull(#{pcu},pcu) where id=#{id}")
    void update(Room room);

    @Select("select id from room where id=#{id} and adminId=#{adminId}")
    Long findByAdminId(Room room);

    @Update("update room set up=ifnull(up,0)+1 where id=#{id}")
    void voteUp(Long id);

    @Update("update room set down=ifnull(down,0)+1 where id=#{id}")
    void voteDown(Long id);

    @Update("update room set flat=ifnull(flat,0)+1 where id=#{id}")
    void voteFlat(Long id);

    @Select("select up, down, flat from room where id=#{id}")
    @Results({
            @Result(property = "up", column = "up"),
            @Result(property = "down", column = "down"),
            @Result(property = "flat", column = "flat")
    })
    Vote findUpDown(Long id);

    @Select("select id from room where adminId=#{userId}")
    List<Long> findListByAdminId(Long userId);


    @Select("SELECT room.*,user.username,user.nickname FROM room,user " +
            "where room.adminId=user.id order by room.createTime limit #{startIndex},#{limit}")
    @Results({
            @Result(property = "adminUsername", column = "username"),
            @Result(property = "adminNickname", column = "nickname")
    })
    List<Room> paging(Room room);

    @Select("SELECT count(1) FROM room,user where room.adminId=user.id")
    Integer pagingCount(Room room);

    /**
     * 跑马灯管理
     */
    @Insert("insert into room_marquee (roomId,role,marquee,creatorId,times) values (#{roomId},#{role},#{marquee},#{creatorId},#{times})")
    void insertMarquee(RoomMarquee roomMarquee);

    @Delete("delete from room_marquee where id=#{id} and creatorId=#{creatorId}")
    void deleteMarquee(Map map);

    @Delete("delete from room_marquee where id=#{id}")
    void deleteMarqueeByAdmin(Integer id);

    @Select("select * from room_marquee where creatorId=#{id}")
    List<RoomMarquee> findMarqueeByUserId(Long id);

    /**
     * 视频管理
     */
    @Insert("insert into teacher_video (creatorId,name,intro,videoUrl,cover) values " +
            "(#{creatorId},#{name},#{intro},#{videoUrl},#{cover})")
    void insertVideo(TeacherVideo teacherVideo);

    @Select("select * from teacher_video where creatorId=#{id}")
    List<TeacherVideo> findVideoByCreatorId(Long id);

    @Update("update teacher_video set name=ifnull(#{name},name),intro=ifnull(#{intro}, intro)," +
            "videoUrl=ifnull(#{videoUrl}, videoUrl),cover=ifnull(#{cover}, cover) " +
            "where id=#{id} and creatorId=#{creatorId}")
    void updateVideo(TeacherVideo teacherVideo);

    @Delete("delete from teacher_video where id=#{id}")
    void deleteVideo(Integer id);

    @Delete("delete from teacher_video where id=#{id} and creatorId=#{creatorId}")
    void deleteVideoByCreatorId(Map map);
}
